package zmee.bid.oa;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import zmee.entity.FrontWorkflowRequestInfoin0;
import zmee.util.OARequestUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author lijiawang
 * @Date 2023/3/17 17:14
 * @Description:合同变更单
 */
public class PmctOutadDagreementOp extends AbstractOperationServicePlugIn {
    private Log log= LogFactory.getLog(PmctOutadDagreementOp.class);
    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        String key = e.getOperationKey();
        DynamicObject[] dataEntities = e.getDataEntities();
        if ("submit".equals(key)) {
            for (DynamicObject data : dataEntities) {
                if(!data.getDynamicObjectType().isForceUpdateModifier()){
                    data = BusinessDataServiceHelper.loadSingle(data.getPkValue(), data.getDynamicObjectType().getName());
                }
                String isnotoa = data.getString("isnotoa");
                if("0".equals(isnotoa)){
                    getWorkflowRequest(data);
                }
            }
        }
    }
    private void getWorkflowRequest(DynamicObject dagreement){
        Map info=new HashMap<>();
        info.put("resource", "优采云");
        DynamicObject user = BusinessDataServiceHelper.loadSingle(dagreement.getDynamicObject("sqr").getPkValue(), "bos_user");
        info.put("usderid", user.getString("username"));
        info.put("accountType", 1);
        info.put("name", "环能实业-合同审批流程");//请求标题
        info.put("workflowName", "环能实业-合同审批流程");//工作流标题
        info.put("workflowId", "3852");//工作流ID
        //info.put("isnextflow", 0);
        Map<String, Object> mainfieldMap = OAutil.getMainfieldMap(dagreement);
        List<Map<String, Object>> attachmentpanels = AttachmentServiceHelper.getAttachments(dagreement.getDynamicObjectType().getName(), dagreement.getPkValue(), "attachmentpanel");
        List<Map<String, String>> stringListMap = OAutil.urlList(attachmentpanels, "fj");
        //单据数据转请求数据
        FrontWorkflowRequestInfoin0 requestInfoin0 = OARequestUtil.getFrontWorkflowRequestInfoin0(info, mainfieldMap, stringListMap);
        //请求参数
        String body = OARequestUtil.workflowRequestBody(requestInfoin0);
        log.info("==补充协议-body==" + body);
        //调用OA审批接口
        String request = OARequestUtil.doCreateWorkflowRequest(body);
        String processing = OARequestUtil.requestProcessing(request);
        log.info("==补充协议-request==" + processing);
        dagreement.set("oaflow",processing);
//        SaveServiceHlper.update(dagreement);

    }
}
